using System.Collections;
using MultiplayerServerConnectionHandlers;
using MultiplayerServerDiscoveryHandlers;
using UnityEngine;

public class ServerDisplayButton : MonoBehaviour
{
	public UILabel serverName;

	private Server server;

	public GameObject selfLayer;

	public GameObject gyroLayer;

	public void Init(Server server)
	{
		this.server = server;
		UITools.ChangeText(serverName, server.serverAddress.ToString() + ":" + server.port);
	}

	public void OnClickConnect()
	{
		StartCoroutine(DoSendConnect());
	}

	private IEnumerator DoSendConnect()
	{
		ConnectionHandler connection = new ConnectionHandler(server.serverAddress, server.port);
		BehaviourSingleton<GGUDPNetworkServer>.instance.AddHandler(connection);
		int doCount = 0;
		UITools.ChangeText(TableTennisDialog.instance.textLabel, "Connecting");
		NavigationManager.instance.PushModal(TableTennisDialog.instance.dialogGameObject);
		while (!connection.isActive)
		{
			yield return null;
		}
		UITools.ChangeText(TableTennisDialog.instance.textLabel, "Connection established");
		yield return new WaitForSeconds(3f);
		NavigationManager.instance.Pop();
		ServerDetectionNavigation nav = NavigationManager.instance as ServerDetectionNavigation;
		if (nav != null)
		{
			nav.Push(nav.controllerCallibrationLayer);
		}
	}
}
